namespace CIS
{
    using CISLib;
    using System;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserManage : Page
    {
        protected DropDownList AreaDdl;
        protected DropDownList catDdl;
        protected Label clbAreaDdl;
        protected Image iSysUserManage;
        protected Label lbArea;
        protected Label lbAreaDdl;
        protected Label lbCat;
        protected Label lbcatDdl;
        protected Label lbCount;
        protected Label lbCountDis;
        protected Label lbLoginName;
        protected Label lbNameDdl;
        protected Label lbOperate;
        protected Label lbProvince;
        protected Label lbprovinceDdl;
        protected Label lbRole;
        protected Label lbRoleDdl;
        protected DropDownList NameDdl;
        protected Button newUserBtn;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected DropDownList RoleDdl;
        protected Repeater rptUser;
        protected Button SearchBtn;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.RoleDdl.Items.Clear();
            //Guid tmpGuid = new Guid();
            this.RoleDdl.DataSource = ManageRole.GetRole(new Guid(this.AreaDdl.SelectedValue));
            this.RoleDdl.DataTextField = "RoleName";
            this.RoleDdl.DataValueField = "RoleId";
            this.RoleDdl.DataBind();
            ListItem roleItem = new ListItem("All", "All");
            this.RoleDdl.Items.Insert(0, roleItem);
        }

        protected string GetCATName(object id)
        {
            return ManageCAT.GetCATViaId((Guid) id).CatName;
        }

        protected string GetRegionName(object id)
        {
            return ManageProvince.GetProvinceViaId((Guid) id).ProvinceName;
        }

        protected string GetRoleName(object id)
        {
            try
            {
                return ManageRole.GetRoleViaId((Guid) id).RoleName;
            }
            catch
            {
                return "";
            }
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("UserManage", str, language, path);
        }

        public string GetUserAreaIntoHtml(object id)
        {
            Area[] area = ManageArea.GetAreaAryViaId((Guid) id);
            int len = (area != null) ? area.Length : 0;
            StringBuilder sb = new StringBuilder();
            if (len == 1)
            {
                sb.Append(area[0].AreaName);
            }
            else if (len > 1)
            {
                sb.Append("<SELECT>");
                for (int i = 0; i < len; i++)
                {
                    sb.Append("<OPTION>");
                    sb.Append(area[i].AreaName);
                    sb.Append("</OPTION>");
                }
                sb.Append("</SELECT>");
            }
            return sb.ToString();
        }

        public string GetUserCatIntoHtml(object id)
        {
            Cat[] cat = ManageCAT.GetCATAryViaId((Guid) id);
            int len = (cat != null) ? cat.Length : 0;
            StringBuilder sb = new StringBuilder();
            if (len == 1)
            {
                sb.Append(cat[0].CatName);
            }
            else if (len > 1)
            {
                sb.Append("<SELECT>");
                for (int i = 0; i < len; i++)
                {
                    sb.Append("<OPTION>");
                    sb.Append(cat[i].CatName);
                    sb.Append("</OPTION>");
                }
                sb.Append("</SELECT>");
            }
            return sb.ToString();
        }

        public string GetUserProvincesIntoHtml(object id)
        {
            Province[] provinces = ManageProvince.GetProvinceAryViaUserId((Guid) id);
            int len = (provinces != null) ? provinces.Length : 0;
            StringBuilder sb = new StringBuilder();
            if (len == 1)
            {
                sb.Append(provinces[0].ProvinceName);
            }
            else if (len > 1)
            {
                sb.Append("<SELECT>");
                for (int i = 0; i < len; i++)
                {
                    sb.Append("<OPTION>");
                    sb.Append(provinces[i].ProvinceName);
                    sb.Append("</OPTION>");
                }
                sb.Append("</SELECT>");
            }
            return sb.ToString();
        }

        private void InitializeComponent()
        {
            this.newUserBtn.Click += new EventHandler(this.newUserBtn_Click);
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            this.rptUser.ItemCommand += new RepeaterCommandEventHandler(this.rptUser_ItemCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.RoleDdl.Items.Clear();
            //Guid tmpGuid = new Guid();
            this.RoleDdl.DataSource = ManageRole.GetRole(new Guid(this.AreaDdl.SelectedValue));
            this.RoleDdl.DataTextField = "RoleName";
            this.RoleDdl.DataValueField = "RoleId";
            this.RoleDdl.DataBind();
            ListItem roleItem = new ListItem("All", "All");
            this.RoleDdl.Items.Insert(0, roleItem);
            this.NameDdl.Items.Clear();
            for (char i = 'A'; i <= 'Z'; i = (char) (i + '\x0001'))
            {
                this.NameDdl.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ListItem spellItem = new ListItem("All", "All");
            this.NameDdl.Items.Insert(0, spellItem);
        }

        private void NameDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.rptUser.DataSource = ManageUser.GetUserViaUserFirstSpellName(this.NameDdl.SelectedItem.Text, new Guid(this.AreaDdl.SelectedValue));
            this.rptUser.DataBind();
        }

        private void newUserBtn_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("UserEdit.aspx", true);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.SetFrom();
                this.InitPage();
                this.SetFromImage();
            }
        }

        private void RoleDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.rptUser.DataSource = ManageUser.GetUserViaRoleId(new Guid(this.RoleDdl.SelectedValue));
            this.rptUser.DataBind();
        }

        private void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strTmp = (string) e.CommandArgument;
            Guid userId = new Guid(strTmp);
            string str = cmd;
            if (str != null)
            {
                str = string.IsInterned(str);
                if (str == "delete")
                {
                    if (userId != Guid.Empty)
                    {
                        Area[] area = ManageArea.GetAreaAryViaId(userId);
                        if ((this.Principal.OwnArea.Length == 1) && (area.Length > 1))
                        {
                            base.Response.Write("<script> alert('没有权限删除') </script>");
                        }
                        else
                        {
                            ManageUser.RemoveUserViaId(userId);
                            this.rptUserDataBind();
                        }
                    }
                }
                else if (str == "modify")
                {
                    if (userId != Guid.Empty)
                    {
                        string url = string.Format("UserEdit.aspx?UserId={0}&IsModify={1}", userId, "True");
                        base.Response.Write("<script>window.open('" + url + "');</script>");
                    }
                }
                else if ((str == "getCustomerAry") && (userId != Guid.Empty))
                {
                    string url = string.Format("CustomerAry.aspx?UserId={0}", userId);
                    base.Response.Redirect(url, true);
                }
            }
        }

        private void rptUserDataBind()
        {
            this.lbCount.Text = ManageUser.GetUserCount(new Guid(this.AreaDdl.SelectedValue), this.provinceDdl.SelectedValue, this.catDdl.SelectedValue, this.RoleDdl.SelectedValue, this.NameDdl.SelectedValue);
            this.rptUser.DataSource = ManageUser.GetUser(new Guid(this.AreaDdl.SelectedValue), this.provinceDdl.SelectedValue, this.catDdl.SelectedValue, this.RoleDdl.SelectedValue, this.NameDdl.SelectedValue);
            this.rptUser.DataBind();
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            this.rptUserDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.newUserBtn.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "newUserBtn", language, path);
            this.lbLoginName.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbLoginName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbProvinceOverSea", language, path);
            }
            else
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbProvince", language, path);
            }
            this.lbCat.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbCat", language, path);
            this.lbRole.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbRole", language, path);
            this.lbOperate.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbOperate", language, path);
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbArea", language, path);
            this.lbAreaDdl.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbAreaDdl", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbprovinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbprovinceDdlOverSea", language, path);
            }
            else
            {
                this.lbprovinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbprovinceDdl", language, path);
            }
            this.lbcatDdl.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbcatDdl", language, path);
            this.lbRoleDdl.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbRoleDdl", language, path);
            this.lbNameDdl.Text = ManageSystemConfig.GetSystemSetLanguage("UserManage", "lbNameDdl", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iSysUserManage.ImageUrl = "images/title_SysUserManage.jpg";
                    break;

                case "en":
                    this.iSysUserManage.ImageUrl = "images/title_SysUserManageEn.jpg";
                    break;
            }
        }
    }
}

